// pages/ui/index/index.js
var that;
var mapContext;
var locationArr;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    head_bg: '../../../res/top_bg.png',
    // userInfo: {
    //   'userHead': '',//用户头像
    //   'userName': '',//用户名
    // },
    userHead:'../../../res/user-head.jpeg',
    userName:'路小猫',
    arrow_right: '../../../res/icon_right.png',//箭头
    activity_banner:'../../../res/activity_banner.png',//活动banner
    icon_charge:'../../../res/icon_charge.png',//充电
    icon_chest: '../../../res/icon_chest.png',//换电
    icon_rent: '../../../res/icon_rent.png',//租车
    icon_hot: '../../../res/icon_hot.png',
    scale: 16,//地图显示级别
    markers: [],
    imgUrls: [
      '../../../res/activity_banner.png',
      '../../../res/activity_banner.png'
    ],
    indicatorDots: true,//指示点
    autoplay: false,
    interval: 5000,//自动切换时间间隔
    duration: 1000 //滑动动画时长
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;//初始化
    //获得地图
    locationArr = [];
    mapContext = wx.createMapContext('map');
    that.getCityLocation();

    //获取用户信息
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          userHead: res.userInfo.avatarUrl,
          userName: res.userInfo.nickName,
        })
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 跳转到我的
   */
  clickMy: function () {
    wx.navigateTo({
      url: '../my/my',
    })
  },

  /**
   * 跳转到我的会员
   */
  clickMyMember: function () {
    wx.navigateTo({
      url: '../mymember/mymember',
    })
  },

  /**
   * 1:对应建议营销Tab 2:对应新增会员Tab 3:对应全部会员Tab
   */
  clickAdd: function() {
    wx.navigateTo({
      url: '../memberlist/memberlist?id=2',
    })
  },
  clickAll: function () {
    wx.navigateTo({
      url: '../memberlist/memberlist?id=3',
    })
  },
  clickSuggest: function () {
    wx.navigateTo({
      url: '../memberlist/memberlist?id=1',
    })
  },

  /**
   * 跳转服务页面
   */
  clickServicePage: function (e) {
    var index = parseInt(e.currentTarget.dataset.index);
    //console.log(index);return;
    wx.navigateTo({
      url: '../service/service?id=' + index,
    })
  },

  /**
   * 获取定位
   */
  getCityLocation: function () {
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        console.log(res);
        var locationString = res.latitude + "," + res.longitude;
        for (var i = 0; i < 9; i++) {
          locationArr.push({
            iconPath: "../../../res/icon_vip.png",
            id: i,
            latitude: res.latitude - 0.003 * (Math.random() - Math.random()),
            longitude: res.longitude + 0.008 * (Math.random() - Math.random()),
            width: 18,
            height: 20
          })
        }
        console.log(locationArr);
        that.setData({
          markers: locationArr,
          lat: res.latitude,
          lon: res.longitude
        })
      },
      fail: function () {
        // fail
        console.log("定位失败");
      },
      complete: function () {
        // complete
        console.log("定位完成");
      }
    })
  },

})